<?php 

class Tag_Bootstrap extends Zend_Application_Module_Bootstrap
{
    /**
     * Seting resources
     *
     */
    protected function _initResources()
    {
        $resources=Zend_Registry::get('resources');
        
        $resources[]=Application_Model_Tags::RESOURCE_NAME; // tags resource
        
        Zend_Registry::set('resources', $resources);
        
        Zend_Registry::get('access')->addResource(Application_Model_Tags::RESOURCE_NAME);
    }
    
    /**
     * Initializing access permissions
     *
     * @return object
     */
    protected function _initAccess()
    {
        $access=Zend_Registry::get('access');

        //seting access permissions for resource
        $access->allow(Application_Model_UserRoles::getDefaultRole(),Application_Model_Tags::RESOURCE_NAME,Application_Model_UserPerms::VIEW_PERM); // allow everybody to view tags
        
        $access->allow(Application_Model_UserRoles::getAdminRole(),Application_Model_Tags::RESOURCE_NAME,Application_Model_UserPerms::MANAGE_PERM); // allow admin to manage tags
    }
    
    protected function _initRoute()
    {  
        $module='tag';
        
        $routes=array(
                    'tag',
                    'tag/:tag',
                    'tag/:tag/:key',
                    'tags',
                    'tags/:key',
                    'tags/add',
                    'tags/delete/:id'
                    );
                    
        foreach ($routes as $id=>$route)
        {
            $controller='index';
            
            if ($id == 3 || $id ==4) $controller='list';
            
            $action='index';
            
            if ($id == 5) $action='add';
            if ($id == 6) $action='delete';
            
            Zend_Registry::get('router')->addRoute($module.$controller.$action.$id, 
                                new Zend_Controller_Router_Route($route,array(
                                    'controller' => $controller,
                                    'action' => $action,
                                    'module' => $module
                                ))
                            ); 
                            
            Zend_Registry::get('router')->addRoute($module.$controller.$action.$id.'language', 
                                new Zend_Controller_Router_Route(':language/'.$route,array(
                                    'controller' => $controller,
                                    'action' => $action,
                                    'module' => $module
                                ))
                            );
        }
    }
    
    protected function _initPlaceholders()
    {
        $view=Zend_Registry::get('view');
        
        $view->placeholder('tags')
             ->setPrefix('<ul id="tags"><li>')
             ->setSeparator('</li><li>')
             ->setPostfix('</li></ul><div class="clear"></div>');
    }
    
    /**
     * Initializing tags sidebar widget
     */
    protected function _initSidebar()
    {
        if (!Zend_Registry::get('user')->canView(new Application_Model_UserResources(Application_Model_Tags::RESOURCE_NAME))) return;
        
        $view=Zend_Registry::get('view');
        
        $uri='';
        if (isset($_SERVER['REQUEST_URI']))
        {
            $_uri=trim(urldecode($_SERVER['REQUEST_URI']),'/');
            if (preg_match('/^page\/(.+?)(\.html)?$/',$_uri,$matches))
            {
                $uri=$matches[1];
            }
        } 
        
        $tagTbl= new Application_Model_TagsDb();
        $tags=$tagTbl->fetch($uri);
        
        if (!empty($tags))
        {
            foreach($tags as $tag)
            {
                $tag=strip_tags($tag);
                $view->placeholder('tags')->append('<a href="'.$view->fixUrl('tag/'.$tag).'" title="'.$tag.'">'.$tag.'</a>');
            }
            
            $view->placeholder('sidebar')->append($view->placeholder('tags'));
        }
    }
}
